package com.fruit.controller.wap.goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.goods.Base;
import com.fruit.pojo.goods.GoodsSpecTime;
import com.fruit.pojo.goods.GoodsSpecTimeHarvest;
import com.fruit.pojo.rights.UserGoodsRights;
import com.fruit.pojo.user.UserInfo;
import com.fruit.resolver.DefiniteException;
import com.fruit.service.goods.GoodsManager;
import com.fruit.service.rights.UserRightsManager;
import com.fruit.service.user.UserManager;
import com.fruit.util.DateUtil;
import com.fruit.util.ResultInfo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping(value = "/wap/share/")
public class ShareControllerWap  extends BaseController {

	@Autowired
	private UserRightsManager userRightsManager;

	@Autowired
	private GoodsManager goodsManager;

	@Autowired
	private UserManager userManager;
	/**
	 * 维护记录分享
	 *
	 * @param id
	 *            权益信息ID
	 * @return Map<String, Object>
	 * @author 葛欢欢
	 * @version 2017.11.30 12:20
	 */
	@RequestMapping(value = "share", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> share(HttpServletRequest request, @RequestParam("id") String id) {
		long a = System.currentTimeMillis();
		log.info("【获取分享页面进入时间】" + DateUtil.getTime());
		log.info("【获取分享页面】---【开始】---");
		Map<String, Object> resMap = new HashMap<>();
		log.info("【获取权益详情ID】 " + id);
		GoodsSpecTime goodsDetail = new GoodsSpecTime();
		if (StringUtils.isBlank(id)) {
			resMap.put("result", this.initResult(ResultInfo.ERROR, "ERROR", "参数错误"));
		} else {
			UserGoodsRights right = userRightsManager.getUserGoodsRight(Integer.parseInt(id));
			resMap.put("img", right.getThumbDay());
			int goodsTimeId = (int)goodsManager.getGoodsTimeIdByGoodsTimeSpecId(right.getUserGoodsRightsId());
			if(goodsTimeId<=0){
				resMap.put("result", this.initResult(ResultInfo.ERROR, "ERROR", "参数错误"));
			}else{
				List<GoodsSpecTime> goodsList = goodsManager.getGoodsDetail(goodsTimeId);
				if (goodsList == null || goodsList.size() == 0) {
					throw new DefiniteException("商品不存在");
				} else {
					goodsDetail = goodsList.get(0); // 详情
					resMap.put("goodsName", goodsDetail.getTitle());
					resMap.put("goodsPrice", goodsDetail.getPrice() + "/" + goodsDetail.getUnitDesc());
					resMap.put("goodsEquityCycle", goodsDetail.getEquityCycle());
					resMap.put("goodsExpectHarvest", goodsDetail.getExpectHarvest() + goodsDetail.getHarvestUnitDesc() + "/" + goodsDetail.getUnitDesc());
					resMap.put("goodsContent", goodsDetail.getContent() );
					resMap.put("baseName", goodsDetail.getBaseName());
				}
				if(right.getUserId()<=0){
					resMap.put("result", this.initResult(ResultInfo.ERROR, "ERROR", "用户不存在"));
				}else{
					UserInfo userInfo = userManager.getUser(right.getUserId());
					resMap.put("userName", userInfo.getPetName());
				}
			}
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "成功"));
		}
		log.info("【获取分享页面信息详情】---【结束】---");
		log.info("【获取分享页面信息详情耗时】" + (System.currentTimeMillis() - a) / 1000f + "秒");
		return resMap;
	}
	
	
	/**
	 * 商品详情页面分享
	 * 
	 * @param request
	 *            request
	 * @param session
	 *            session
	 * @return ModelAndView
	 * @author 葛欢欢
	 * @version 2017.11.30 12:20
	 */
	@RequestMapping(value = "/shareGoods", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ModelAndView shareGoods(HttpServletRequest request, HttpSession session) {
		long a = System.currentTimeMillis();
		log.info("【商品详情页面分享进入时间】" + DateUtil.getTime());
		log.info("【商品详情页面分享】---【开始】---");
		String goodsId = request.getParameter("id");
		log.info("【获取商品ID】---" + goodsId);
		if (StringUtils.isBlank(goodsId)) {
			throw new DefiniteException("商品id为空");
		}
		ModelAndView view = this.getModelAndView();
		List<GoodsSpecTime> goodsList = goodsManager.getGoodsDetail(Integer.parseInt(goodsId)); // 详情
		if (goodsList == null || goodsList.size() == 0) {
			throw new DefiniteException("商品不存在");
		} else {
			GoodsSpecTime goodsDetail = goodsList.get(0); // 详情
			log.info("【获取一条商品信息】---" + goodsDetail);
			List<GoodsSpecTimeHarvest> gsth = goodsManager.findGoodsSpecTimeHarvest(goodsDetail.getId());
			int harvestNum = 0;
			if(gsth!=null && gsth.size()!=0){
				harvestNum = gsth.size();
				for(int i=0;i<gsth.size();i++){
					GoodsSpecTimeHarvest gsv = gsth.get(i);
					String harvesTimeStr = DateUtil.dateTimeToYYD(gsv.getExpectHarvestTime());
					gsv.setExpectHarvestTime(harvesTimeStr.substring(0, 8));
				}
			}else{
				harvestNum = 0;
			}
			String[] img = StringUtils.split(goodsDetail.getPicture(), ",");
			int imgNum = img.length;
			if(imgNum>1){
				view.addObject("img", img); // 图片
			}else{
				view.addObject("imgOne", img[0]); // 图片
			}
			Integer userId = (Integer) session.getAttribute("userId");
			log.info("【获取用户id】---" + userId);

			boolean dateB = DateUtil.compareDate(DateUtil.dTime(goodsDetail.getEndTime()), DateUtil.getDay());
			if (dateB) { // true 表示当前系统时间 小于 商品下架时间一样 （商品详情页面的 今日认养时间 为当前系统时间）
				view.addObject("date", DateUtil.getsdfDaymd());
			} else { // false 表示当前系统时间 大于等于 商品下架时间一样 （商品详情页面的 今日认养时间 为下架时间）
				view.addObject("date", DateUtil.dateTime(goodsDetail.getEndTime()));
			}
			view.addObject("goodsDetail", goodsDetail); // 商品详情
			view.addObject("imgNum", imgNum); //图片数量
			view.addObject("userId", userId);
			view.addObject("goodsTimeId", goodsId);
			view.addObject("gsth", gsth); // 预计产量
			view.addObject("harvestNum", harvestNum); // 预计产量批数
			view.setViewName("good/detail");
		}
		log.info("【商品详情页面分享】---【结束】---");
		log.info("【商品详情页面分享耗时】" + (System.currentTimeMillis() - a) / 1000f + "秒");
		return view;
	}
	
}
